import router from './router'
import store from './store'
import axios from 'axios'
import { Message } from 'element-ui';
const whiteList = ['/login','/reg']
router.beforeEach((to,from,next)=>{
  if (whiteList.includes(to.path)) return next()
  if(store.state.token) return next()
  next('/login')
  
})

axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  if(store.state.token) {
    config.headers.Authorization=store.state.token
  }
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response;
}, function (error) {
  // 对响应错误做点什么
  if(error.response.status===401){
    store.token = ''
    router.push('login')
    Message.error('用户登录失效')
  }
  console.log(error.response);
  return Promise.reject(error);
});